﻿using System;
using System.Collections.Generic;

using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Hosting;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Autofac;
using Autofac.Integration.Mvc;
using Autofac.Integration.WebApi;
using Microsoft.AspNet.SignalR;
using eCommMongo.Core;
using eCommMongo.Core.Data;
using eCommMongo.Data;
using eCommMongo.Service;
using eCommMongo.Service.Authentication;
using eCommMongo.Service.Customers;
using eCommMongo.Service.FriendShips;
using eCommMongo.Service.Loggings;
using eCommMongo.Service.Messages;
using eCommMongo.Service.Pictures;
using eCommMongo.Service.Posts;
using eCommMongo.Service.Products;
using eCommMongo.Service.Securitys;
using eCommMongo.Service.Statuses;
using eCommMongo.Service.Tags;
using yiCommerce.Service.Messages;
using Autofac.Integration.SignalR;

namespace eCommMongo.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            //initialize engine context
            var container = BuildContianer();
            EngineContext.Initialize(false, container);


            AreaRegistration.RegisterAllAreas();
            RouteTable.Routes.MapHubs();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();


            //var embeddedViewResolver = new EmbeddedViewResolver();
            //var embeddedProvider = new EmbeddedViewVirtualPathProvider(embeddedViewResolver.GetEmbeddedViews());
            //HostingEnvironment.RegisterVirtualPathProvider(embeddedProvider);
        }

        private IContainer BuildContianer()
        {
            var builder = new ContainerBuilder();
            // Configure the container  
            builder.RegisterHubs(Assembly.GetCallingAssembly());

            //db
            builder.RegisterGeneric(typeof (MongoRepository<>)).As(typeof (IMongoRepository<>)).InstancePerHttpRequest();
            //builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).InstancePerHttpRequest();
            //builder.Register<DbContext>(c => new YiObjectContext()).InstancePerHttpRequest();


            //service
            //builder.RegisterType<AddressService>().As<IAddressService>().InstancePerHttpRequest();
            builder.RegisterType<FormsAuthenticationService>().As<IAuthenticationService>();
            builder.RegisterType<WebWorkContext>().As<IWorkContext>().InstancePerHttpRequest();
            builder.RegisterType<CustomerService>().As<ICustomerService>();
            builder.RegisterType<PictureService>().As<IPictureService>();
            builder.RegisterType<EncryptionService>().As<IEncryptionService>();
            builder.RegisterType<MessageService>().As<IMessageService>().InstancePerHttpRequest();
            builder.RegisterType<CustomerRegistrationService>().As<ICustomerRegistrationService>();
            builder.RegisterType<CommentService>().As<ICommentService>();
            builder.RegisterType<PostService>().As<IPostService>();
            builder.RegisterType<FriendShipService>().As<IFriendShipService>();
            builder.RegisterType<TagService>().As<ITagService>();
            builder.RegisterType<ProductService>().As<IProductService>();
            builder.RegisterType<ProductSearchService>().As<IProductSearchService>();
            builder.RegisterType<PermissionService>().As<IPermissionService>();
            builder.RegisterType<ActivityService>().As<IActivityService>();

            //controller
            var callingAssembly = Assembly.GetCallingAssembly();
            builder.RegisterControllers(callingAssembly);

            builder.RegisterApiControllers(callingAssembly);


            //builder.RegisterType<StatusesHub>().ExternallyOwned();

            var container = builder.Build();
            //var resolver = new AutofacWebApiDependencyResolver(container);
            var resolver = new Autofac.Integration.Mvc.AutofacDependencyResolver(container);
            DependencyResolver.SetResolver(resolver);
            GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

            GlobalHost.DependencyResolver = new Autofac.Integration.SignalR.AutofacDependencyResolver(container); ;
            return container;

        }
    }
}